package com.ruoyi.quartz.task;

import com.ruoyi.business.domain.TDSsq;
import com.ruoyi.business.domain.TDSsqBet;
import com.ruoyi.business.service.ITDSsqBetService;
import com.ruoyi.business.service.ITDSsqService;
import com.ruoyi.common.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITDSsqService ssqService;
    @Autowired
    private ITDSsqBetService ssqBetService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    /**
     * 双色球抓取任务
     */
    public void mySsq(){
        logger.info("双色球抓取任务开始");
        List<LinkedHashMap<String,Object>> list = SsqHttp.ssqhistory();
        if (list !=null && list.size()>0){
            TDSsq ssq=null;
            for (LinkedHashMap<String,Object> item:list){
                ssq=new TDSsq();
                ssq.setId(Integer.valueOf(item.get("id").toString()));
                TDSsq s= ssqService.selectTDSsqById(ssq.getId());
                if (s == null){
                    ssq.setOne(item.get("one").toString());
                    ssq.setTwo(item.get("two").toString());
                    ssq.setThree(item.get("three").toString());
                    ssq.setFour(item.get("four").toString());
                    ssq.setFive(item.get("five").toString());
                    ssq.setSix(item.get("six").toString());
                    ssq.setSeven(item.get("seven").toString());
                    ssq.setBonusPool(item.get("bonusPool").toString());
                    ssq.setOneBonus(item.get("oneBonus").toString());
                    ssq.setOneNotes(Integer.valueOf(item.get("oneNotes").toString()));
                    ssq.setTwoBonus(item.get("twoBonus").toString());
                    ssq.setTwoNotes(Integer.valueOf(item.get("twoNotes").toString()));
                    ssq.setTotalBonus(item.get("totalBonus").toString());
                    ssq.setOpenBounsDate(DateUtils.parseDate(item.get("openDate")));
                    ssqService.insertTDSsq(ssq);
                }

            }
        }
        logger.info("双色球抓取任务结束");
    }

    /**
     * 定时任务每天一发
     */
    public void randSsq(){
        //每天一注随机
        TDSsqBet ssqBet=new TDSsqBet();
        for (int i=0 ;i<5;i++){
            //获取最近一期的期号
            List<Integer> list= SsqJsoup.shuff();
            int lastNum=ssqService.findLastNum();
            ssqBet.setStage(lastNum+1);
            ssqBet.setOne(convNum(list.get(0)));
            ssqBet.setTwo(convNum(list.get(1)));
            ssqBet.setThree(convNum(list.get(2)));
            ssqBet.setFour(convNum(list.get(3)));
            ssqBet.setFive(convNum(list.get(4)));
            ssqBet.setSix(convNum(list.get(5)));
            ssqBet.setSeven(convNum(list.get(6)));
            ssqBet.setBet(1);
            ssqBet.setCreateTime(new Date());
            ssqBet.setUpdateTime(new Date());
            ssqBetService.insertTDSsqBet(ssqBet);
        }

    }

    /**
     * 定时兑奖
     */
    public void cash(){
        //查询状态为0
        TDSsqBet bet=new TDSsqBet();
        bet.setState(0);
        List<TDSsqBet> betList= ssqBetService.selectTDSsqBetList(bet);
        if (betList !=null && betList.size()>0){
            for (TDSsqBet item:betList){
                TDSsq ssq=ssqService.selectTDSsqById(item.getStage());
                if (ssq==null){
                    continue;
                }
                //查询开奖期号的彩票
                String[] round= new String[7];
                round[0]=item.getOne();
                round[1]=item.getTwo();
                round[2]=item.getThree();
                round[3]=item.getFour();
                round[4]=item.getFive();
                round[5]=item.getSix();
                round[6]=item.getSeven();
                String[] history=new String[7];
                history[0]=ssq.getOne();
                history[1]=ssq.getTwo();
                history[2]=ssq.getThree();
                history[3]=ssq.getFour();
                history[4]=ssq.getFive();
                history[5]=ssq.getSix();
                history[6]=ssq.getSeven();
                item.setAward( betWin(round,history));
                item.setState(1); //表示已开过奖下次不会再查询了
                item.setUpdateTime(new Date());
                int a1=Integer.valueOf(ssq.getOneBonus().replaceAll(",",""));
                int a2=Integer.valueOf(ssq.getTwoBonus().replaceAll(",",""));
                item.setWinMoney(jianjin(item.getAward(),item.getBet(),a1,a2));
                item.setOpenDate(ssq.getOpenBounsDate());
                ssqBetService.updateTDSsqBet(item);
            }
        }
    }

    protected String convNum(int num){
        if (String.valueOf(num).length()==1){
            return "0"+num;
        }
        return String.valueOf(num);
    }

    protected int betWin(String[] curBet,String[] beforBet){
        int bule=0,red=0;
        for (int i=0;i<6;i++){
            if (curBet[i].equals(beforBet[i])){
               bule++;
            }
        }
        if (curBet[6].equals(beforBet[6])){
            red++;
        }
        if (bule==2 && red==1){
            return 6;
        }else if (bule==1 && red ==1){
            return 6;
        }else if (bule==0 && red == 1){
            return 6;
        }
        if ( (bule ==3 && red==1) || (bule==4 && red==0)){
            return  5;
        }
        if ((bule==4 && red==1) || (bule==5 && red==0)){
            return 4;
        }
        if (bule == 5 && red==1){
            return 3;
        }
        if (bule ==6 && red ==0){
            return 2;
        }
        if (bule == 6 && red ==1){
            return 1;
        }
        return 7;
    }

    protected int jianjin(int win,int bet,int one,int two){
        int total=0;
        switch (win){
            case 1:
                total= bet*one;
                break;
            case 2:
                total=bet*two;
                break;
            case 3:
                total=bet*3000;
                break;
            case 4:
                total=bet*200;
                break;
            case 5:
                total=bet*10;
                break;
            case 6:
                total=bet*5;
                break;

        }
        return total;
    }
}
